package com.mornd.gulimall.member.interceptor;

import com.mornd.gulimall.common.core.auth.UserHolder;
import com.mornd.gulimall.common.core.constant.LoginConstant;
import com.mornd.gulimall.common.core.entity.SessionUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: mornd
 * @dateTime: 2023/8/21 - 21:43
 */
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        HttpSession session = request.getSession();
//        SessionUser sessionUser = (SessionUser) session.getAttribute(LoginConstant.SESSION_USER);
//        if(sessionUser == null) {
//            response.sendRedirect(ServerUrls.AUTH);
//            return false;
//        } else {
//            UserHolder.setUser(sessionUser);
//            return true;
//        }

        // todo del
        SessionUser sessionUser = new SessionUser();
        sessionUser.setId(1L);
        sessionUser.setIntegration(101);
        sessionUser.setNickname("张三_test");
        UserHolder.setUser(sessionUser);
        request.getSession().setAttribute(LoginConstant.SESSION_USER, sessionUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        UserHolder.remove();
    }
}
